/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.msk.sys.utils;

import com.google.common.collect.Maps;
import com.msk.common.utils.StringUtils;
import com.msk.common.utils.spring.SpringUtils;
import com.msk.sys.domain.Office;
import com.msk.sys.mapper.OfficeDao;
import java.util.Map;

/**
 * 部门或公司工具类
 * @author xiangyou
 * @version 2015-12-04
 */
public class OfficeUtils {

	private static OfficeDao officeDao = SpringUtils.getBean(OfficeDao.class);

	public static final String CACHE_OFFICE_MAP = "officeMap";

	public static final String CACHE_OFFICE_NAME_MAP = "officeNameMap";

	public static final String CACHE_OFFICE_CODE_MAP = "officeCodeMap";


	/**
	 * 根据部门ID获取部门
	 * @param officeId
	 * @return Office
	 * @throws
	 */
	public static Office getOffice(String officeId) {
		Office office = new Office();
		if (StringUtils.isEmpty(officeId)) {
			return office;
		}
		@SuppressWarnings("unchecked")
		Map<String, Office> officeMap = Maps.newHashMap();;
		if (officeMap == null) {
			for (Office off : officeDao.findAllList(new Office())) {
				if (office != null && !StringUtils.isEmpty(off.getCode())) {
					officeMap.put(off.getId(), off);
				}
			}
		}
		office = officeMap.get(officeId);
		return office;
	}

	/**
	 * 根据部门ID获取部门
	 * @return Office
	 * @throws
	 */
	public static Office getOfficeByName(String officeName) {
		Office office = new Office();
		if (StringUtils.isEmpty(officeName)) {
			return office;
		}
		Map<String, Office> officeMap = Maps.newHashMap();
		for (Office off : officeDao.findAllList(new Office())) {
			if (office != null && !StringUtils.isEmpty(off.getCode())) {
				officeMap.put(off.getName(), off);
			}
		}
		office = officeMap.get(officeName);
		return office;
	}

	/**
	 * 根据部门ID获取部门
	 * @return Office
	 * @throws
	 */
	public static Office getOfficeByCode(String officeCode) {
		Office office = new Office();
		if (StringUtils.isEmpty(officeCode)) {
			return office;
		}
		Map<String, Office> officeMap = Maps.newHashMap();
		for (Office off : officeDao.findAllList(new Office())) {
			if (office != null && !StringUtils.isEmpty(off.getCode())) {
				officeMap.put(off.getCode(), off);
			}
		}
		office = officeMap.get(officeCode);
		return office;
	}
}
